package _01_第一季.动态数组01;

import _01_第一季._00_公共接口.AbstractList;

@SuppressWarnings("unchecked")
public class ArrayList<E> extends AbstractList<E>{
    private E[] elements;
    private static final int DEFAULT_SIZE = 10;

    public ArrayList() {
        this(DEFAULT_SIZE);
    }

    public ArrayList(int capacity) {
        capacity = Math.max(capacity,DEFAULT_SIZE);
        elements = (E[]) new Object[capacity];
        size = 0;
    }

    @Override
    public void add(int index, E element) {
        rangeCheckForAdd(index);
        enSureCapacity(size+1);

        for(int i=size;i>index;--i){
            elements[i] = elements[i-1];
        }
        elements[index] = element;
        size++;

    }

    //扩容的方法
    private void enSureCapacity(int capacity) {
        int oldCapacity = elements.length;
        if(oldCapacity >= capacity) return;

        int newCapacity = oldCapacity + (oldCapacity >> 1);
        E[] newElements = (E[]) new Object[newCapacity];
        for(int i=0;i<size;++i){
            newElements[i] = elements[i];
        }
        elements = newElements;
        System.out.println("oldCapacity:"+oldCapacity+"扩容为:"+newCapacity);

    }

    @Override
    public void remove(E element) {
        int index = indexOf(element);
        if(index!=ELEMENT_NOT_FOUND){
            remove(index);
        }else{
            throw new IllegalArgumentException("没有该元素");
        }

    }

    public int getLength(){
        return elements.length;
    }

    @Override
    public E remove(int index) {
        rangeCheck(index);
        E ret = elements[index];
        for(int i=index+1;i<size;++i){
            elements[i-1] = elements[i];
        }
        elements[--size] = null;

        trim();

        return ret;
    }

    //缩容的方法
    private void trim() {
        int oldCapacity = elements.length;
        int newCapacity = oldCapacity >> 1;
        if(size >= newCapacity || oldCapacity <= DEFAULT_SIZE) return;

        E[] newElements = (E[]) new Object[newCapacity];
        for(int i=0;i<size;++i){
            newElements[i] = elements[i];
        }
        elements = newElements;

        System.out.println(oldCapacity + "缩容为:"+newCapacity);
    }

    @Override
    public E get(int index) {
        rangeCheck(index);
        return elements[index];
    }

    @Override
    public E set(int index, E element) {
        rangeCheck(index);
        E ret = elements[index];
        elements[index] = element;
        return ret;
    }

    @Override
    public int indexOf(E element) {
        if(element == null){
            for(int i=0;i<size;++i){
                if(elements[i] == null) return i;
            }
        }else{
            for(int i=0;i<size;++i){
                if(element.equals(elements[i])) return i;
            }
        }
        return ELEMENT_NOT_FOUND;
    }

    @Override
    public void clear() {
        for(int i=0;i<size;++i){
            elements[i] = null;
        }
        size = 0;

        if(elements!=null && elements.length >DEFAULT_SIZE){
            elements = (E[]) new Object[DEFAULT_SIZE];
        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder(String.format("ArrayList:%d/%d[",size,elements.length));
        if(isEmpty()){
            sb.append("]");
        }else{
            for(int i=0;i<size;++i){
                sb.append(elements[i]);
                if(i!=size-1){
                    sb.append(",");
                }else {
                    sb.append("]");
                }
            }
        }
        return sb.toString();
    }
}
